package com.hualan.service;

import com.alibaba.fastjson.JSON;
import com.hualan.entity.XiaoQu;
import com.hualan.entity.YiZhan;
import com.hualan.utils.EntityFactory;
import com.hualan.utils.ExecUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = {"/zeng","/shan","/gai","/cha"})
public class StageService extends HttpServlet {
    private List<YiZhan> yiZhans;

    @Override
    public void init() throws ServletException {
        this.yiZhans = new ArrayList<>();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.yiZhans.clear();
        if (req.getServletPath().equals("/cha")){
            List<Object> params = new ArrayList<>();
            String sql;
            if (req.getParameter("courierStationName")!=null&&req.getParameter("courierStationCommunityName")!=null&&req.getParameter("addTime")!=null){
                sql = "select * from stage where courierStationName=? and courierStationCommunityName=? and addTime=?";
                params.add(req.getParameter("courierStationName"));
                params.add(req.getParameter("courierStationCommunityName"));
                params.add(req.getParameter("addTime"));
            } else if (req.getParameter("courierStationName")==null&&req.getParameter("courierStationCommunityName")==null&&req.getParameter("addTime")==null) {
                sql = "SELECT s.*, x.* FROM stage s, xiaoqu x where s.courierStationCommunityId = x.communityId;";
            }else if (req.getParameter("courierStationName")==null&&req.getParameter("courierStationCommunityName")!=null&&req.getParameter("addTime")!=null) {
                sql = "select * from stage where courierStationCommunityName=? and addTime=?";
                params.add(req.getParameter("courierStationCommunityName"));
                params.add(req.getParameter("addTime"));
            }else if (req.getParameter("courierStationName")!=null&&req.getParameter("courierStationCommunityName")==null&&req.getParameter("addTime")!=null) {
                sql = "select * from stage where courierStationName=? and addTime=?";
                params.add(req.getParameter("courierStationName"));
                params.add(req.getParameter("addTime"));
            }else if (req.getParameter("courierStationName")!=null&&req.getParameter("courierStationCommunityName")!=null&&req.getParameter("addTime")==null) {
                sql = "select * from stage where courierStationName=? and courierStationCommunityName=?";
                params.add(req.getParameter("courierStationName"));
                params.add(req.getParameter("courierStationCommunityName"));
            }else if (req.getParameter("courierStationName")==null&&req.getParameter("courierStationCommunityName")==null&&req.getParameter("addTime")!=null){
                sql = "select * from stage where addTime=?";
                params.add(req.getParameter("addTime"));
            }else if (req.getParameter("courierStationName")==null&&req.getParameter("courierStationCommunityName")!=null&&req.getParameter("addTime")==null){
                sql = "select * from stage where courierStationCommunityName=?";
                params.add(req.getParameter("courierStationCommunityName"));
            }else {
                sql = "select * from stage where courierStationName=?";
                params.add(req.getParameter("courierStationName"));
            }
            try {
                ResultSet resultSet = (ResultSet) ExecUtil.execute(sql, params, req, false);
                //4.提取结果集中的数据
                while (resultSet.next()) {
                    EntityFactory entityFactory = new EntityFactory(YiZhan.class);
                    YiZhan yiZhan = (YiZhan) entityFactory.startCombine(resultSet);
                    this.yiZhans.add(yiZhan);
                }
                resp.getWriter().write(JSON.toJSONString(this.yiZhans));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }finally {
                try {
                    ExecUtil.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (req.getServletPath().equals("/zeng")){
            List<Object> params = new ArrayList<>();
            String sql = "insert into stage(courierStationName,courierStationProvince,courierStationCity,courierStationDistrict,courierStationAddress,addTime,courierStationCommunityId,courierStationCommunityName) values(?,?,?,?,?,?,?,?)";
            params.add(req.getParameter("courierStationName"));
            params.add(req.getParameter("courierStationProvince"));
            params.add(req.getParameter("courierStationCity"));
            params.add(req.getParameter("courierStationDistrict"));
            params.add(req.getParameter("courierStationAddress"));
            params.add(req.getParameter("addTime"));
            params.add(req.getParameter("courierStationCommunityId"));
            params.add(req.getParameter("courierStationCommunityName"));
            ExecUtil.execute(sql, params, req, true);
            ExecUtil.commit();
            resp.getWriter().write(params.toString());
            resp.getWriter().write("添加成功");

        } else if (req.getServletPath().equals("/shan")) {
            String sql = "delete from stage where courierStationId=?";
            List<Object> params = new ArrayList<>();
            params.add(req.getParameter("courierStationId"));
            ExecUtil.execute(sql, params, req, true);
            ExecUtil.commit();
        } else if (req.getServletPath().equals("/gai")) {
            List<Object> params = new ArrayList<>();
            String sql = "update stage set courierStationName=?,courierStationProvince=?,courierStationCity=?,courierStationDistrict=?,courierStationAddress=?,addTime=?,courierStationCommunityId=?,courierStationCommunityName=? where courierStationId=?";
            params.add(req.getParameter("courierStationName"));
            params.add(req.getParameter("courierStationProvince"));
            params.add(req.getParameter("courierStationCity"));
            params.add(req.getParameter("courierStationDistrict"));
            params.add(req.getParameter("courierStationAddress"));
            params.add(req.getParameter("addTime"));
            params.add(req.getParameter("courierStationCommunityId"));
            params.add(req.getParameter("courierStationCommunityName"));
            params.add(req.getParameter("courierStationId"));
            ExecUtil.execute(sql, params, req, true);
            ExecUtil.commit();
            resp.getWriter().write(params.toString());
            resp.getWriter().write("修改成功");

        }
    }
}
